<template>
    <div>
        <div id="app">
        <fieldset>
            <legend>{{isLogin?'内容界面':'登入界面'}}</legend>
            
            <!-- 内容展示界面 -->
            <div class="content" v-if="isLogin">
                <h1>列表展示</h1>
                <button @click="isLogin=false">点击退出登入</button>
                <button @click="getList">点击获取列表数据</button>
                <h2>
                    <img :src="userInfo.headimgurl" width="50" alt="">
                    昵称：
                    <a href="">{{userInfo.nickname}}</a>
                </h2>
                <ul>
                    <li v-for="item in lists" :key="item._id">
                        <b>{{item.name}}</b>
                        <button v-has:read="permissionBtns">查看</button>
                        <button v-has:edit="permissionBtns">修改</button>
                        <button v-has:delete="permissionBtns">删除</button>
                        <button v-has:add="permissionBtns">添加</button>
                    </li>
                </ul>
            </div>
            <!-- 登入界面 -->
            <div class="login" v-else>
                <h1>请登入</h1>
                <label for="username">用户名:</label>
                <input type="text" id="username" v-model="username">
                <br>
                <label for="password">密码:</label>
                <input type="password" v-model="password" @keydown.enter="login">
                <br>
                <button @click="login">登入</button>
            </div>
        </fieldset>
        </div>
    </div>
</template>
<script>
//1.页面加载展示登录页面  登录成功 展示内容界面
//2.登入成功的页面切换 登入失败的提示
//3.在登入成功页面 根据todos数据 展示列表
//4.根据登入角色的权限 展示对应的按钮权限
export default {

    directives: {
        has:{
            inserted(el,binding){
                // console.log(el)
                // el.disabled = true
                // console.log(binding)
                //按钮权限
                let role = binding.arg
                //用户权限
                let userRole = binding.value
                //判断用户有没有按钮权限
                let hasRole = userRole.includes(role)

                if(!hasRole){
                    //没有禁用按钮
                    // el.disabled = true
                    //直接删除按钮
                    el.parentNode.removeChild(el)

                }
            }
        }
    },

    methods: {
        //封装一个登入的请求方法
        loginApi(params) {
            return fetch('http://chst.vip/users/login', {
                method: 'post',
                headers: {
                    'content-type': 'application/json'
                },
                body: JSON.stringify(params)
            })
                .then(body => body.json())
                .catch(err => err.toString())
        },
        //封装获取列表请求方法
        getListApi() {
            return fetch('http://chst.vip/students/getstulist?count=4', {
                headers: {
                    'authorization': sessionStorage.getItem('user-token')
                }
            })
                .then(body => body.json())
                .catch(err => err.toString())
        },
        //使用async await 来等待请求的异步结果
        async login() {
            let res = await this.loginApi({ username: this.username, password: this.password })
            console.log(res)

            //判断进行跳转页面
            if (res.state) {
                //登录成功
                this.isLogin = true
                this.username = ''
                this.password = ''
                this.userInfo = res.userInfo
                //将用户的令牌存到本地
                sessionStorage.setItem('user-token', res.token)
                this.permissionBtns = res.permission.buttons

            } else {
                alert(res.msg)
            }

        },
        //获取列表
        async getList() {
            let res = await this.getListApi()
            //判断是否请求成功
            if (res.state) {
                //请求成功
                this.lists = res.data

            } else {
                alert(res.msg)
            }

        }

    },
    data() {
        return {
            //是否登入状态
            isLogin: false,
            password: '',
            username: '',
            //userInfo用户信息
            userInfo: {},

            lists: [],
            //用户权限按钮
            permissionBtns: []



        }
    }
}
</script>
<style scoped>
    ul{
        padding: 0;
        margin: 0;
        list-style: none;
    }
    li{
        border: 1px solid deepskyblue;
        margin: 10px;
        max-width: 500px;
    }

</style>